﻿using System;
using System.Xml.Linq;
using System.Linq;
using System.Xml.Serialization;
using QDP.Platform.Silverlight.Manager;
public static class ConfigExtensions
{
    public static void RequestCatalogDirs(this ConfigManager thiz, Action<bool, string[]> Action)
    {
        if (Action == null) return;
        thiz.Request(thiz.GetUrl("catalog_dirs"), (success, actionArgs) =>
        {
            if (success == false) D.Debug(actionArgs);
            if (success == false || string.IsNullOrEmpty(actionArgs))
            {
                Action(true, new string[0]);
            }
            else
            {
                Action(true, actionArgs.Split(';'));
            }
        });
    }
    public static void RequestCatalogImages(this ConfigManager thiz,string path, Action<bool, string[]> Action)
    {
        if (Action == null) return;
        thiz.Request(thiz.GetUrl("catalog_imgs") + path, (success, actionArgs) =>
        {
            if (success == false) D.Debug(actionArgs);
            if (success == false || string.IsNullOrEmpty(actionArgs))
            {
                Action(true, new string[0]);
            }
            else
            {
                Action(true, actionArgs.Split(';'));
            }
        });
    }
    public static void RequestColors(this ConfigManager thiz,Action<bool, string[]> Action)
    {
        if (Action == null) return;
        thiz.Request(thiz.GetUrl("colors"), (success, actionArgs) =>
        {
            if (success == false)
            {
                D.Debug(actionArgs);
                Action(false, null);
            }
            try
            {
                XDocument doc = XDocument.Parse(actionArgs);

                var d = from element in doc.Root.Elements()
                        select element.Attribute("value").Value;

                Action(true, d.ToArray());
            }
            catch (Exception ex)
            {
                D.Debug(ex.Message);
            }
        });
    }
}
